//
//  ZGMainController.m
//  NewBee
//
//  Created by leergou on 16/8/20.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGMainController.h"
#import "ZGHomeController.h"
#import "ZGSupermarketController.h"
#import "ZGShoppingCarController.h"
#import "ZGProfileController.h"

@interface ZGMainController () <UITabBarControllerDelegate>

@end

@implementation ZGMainController

#pragma mark - 视图声明周期 -
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.tabBar.translucent = NO;
    
    [self setupChildVc];
    // 需要设置代理
    self.delegate = self;
}

#pragma mark - item点击事件 -
- (void)tabBarButtonClick:(UIControl *)sender{
    
    for (UIView *imageView in sender.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据需求自定义
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            animation.values = @[@1.0, @1.4, @0.8,@1.2, @0.8, @1.0];
            animation.duration = 0.5;
            animation.calculationMode = kCAAnimationCubic;
            //把动画添加上去就OK了
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}


#pragma mark - UITabBarControllerDelegate -
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    
    NSInteger index =  [self.childViewControllers indexOfObject:viewController];
    
    
    [self.tabBar.subviews enumerateObjectsUsingBlock:^(UIControl  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [obj addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }];
    
    
    if (index == 2) {
        ZGBaseNavigationController *shopingCarNav = [[ZGBaseNavigationController alloc] initWithRootViewController:[[ZGShoppingCarController alloc] init]];
        
        [self presentViewController:shopingCarNav animated:YES completion:^{
            
        }];
        return NO;
    }
    return YES;
    
}


#pragma mark - 设置子控制器 -
- (void)setupChildVc{
    
    // 首页
    ZGBaseNavigationController *homeNav = [[ZGBaseNavigationController alloc] initWithRootViewController:[[ZGHomeController alloc] init]];
    
    [self setupViewController:homeNav title:@"首页" imgNormal:@"v2_home" imgSelected:@"v2_home_r"];
    
    // 闪电超市
    ZGBaseNavigationController *marketNav = [[ZGBaseNavigationController alloc] initWithRootViewController:[[ZGSupermarketController alloc] init]];
    
    [self setupViewController:marketNav title:@"闪电超市" imgNormal:@"v2_order" imgSelected:@"v2_order_r"];
    
    // 购物车
    ZGBaseNavigationController *shopingCarNav = [[ZGBaseNavigationController alloc] initWithRootViewController:[[ZGShoppingCarController alloc] init]];
    
    [self setupViewController:shopingCarNav title:@"购物车" imgNormal:@"shopCart" imgSelected:@"shopCart_r"];
    
    // 我的
    ZGProfileController *profileVc = [[ZGProfileController alloc] init];
    
    [self setupViewController:profileVc title:@"我的" imgNormal:@"v2_my" imgSelected:@"v2_my_r"];
    
    // 添加到 tabBar
    self.viewControllers = @[homeNav, marketNav, shopingCarNav, profileVc];
}

#pragma mark - 设置导航控制器 -
#pragma mark - 设置tabBarItem
- (void)setupViewController:(UIViewController *)nav title:(NSString *)title imgNormal:(NSString *)imgNormal imgSelected:(NSString *)imgSelected{
    
    if (title) {
        nav.tabBarItem.title = title;
        nav.title = title;
    }
    
    if (imgNormal) {
        [nav.tabBarItem setImage:[UIImage imageNamed:imgNormal]];
    }
    
    if (imgSelected) {
        [nav.tabBarItem setSelectedImage:[UIImage imageNamed:imgSelected]];
    }
}




@end














